import java.awt.Point;
import java.util.ArrayList;
import java.util.StringTokenizer;

import com.speljohan.rsbot.script.Random;
import com.speljohan.rsbot.script.wrappers.RSInterfaceChild;
import com.speljohan.rsbot.script.wrappers.RSInterfaceComponent;

public class ImprovedRewardsBox extends Random {

	private final int boxID = 14664;
	private final int boxIF = 202;
	private final int boxChoicesIF = 15;
	private final int boxConfirmIF = 28;
	private final int boxScrollIF = 24;

	private final int XPIF = 134;
	private final int XPConfirmIF = 2;

	private ArrayList<String> yourChoices;
	private ArrayList<String> rewards;

	private String yourChoice;

	private int wantedSkill;
	private int wanted;

	private final String[] skills = { "Attack", "Strength", "Ranged", "Magic",
			"Defence", "Crafting", "Hitpoints", "Prayer", "Agility",
			"Herblore", "Thieving", "Fishing", "Runecrafting", "Slayer",
			"Farming", "Mining", "Smithing", "Hunter", "Cooking", "Firemaking",
			"Woodcutting", "Fletching", "Construction", "Summoning" };
	private final int lampID = 2528;
	private final int bookOfKnowledgeID = 11640;
	private final int mysteryBoxID = 6199;

	@Override
	public boolean activateCondition() {
		if (isLoggedIn() && inventoryContains(boxID)) {
			yourChoice = getAccountProperty("reward");
			return true;
		}
		return false;
	}

	@Override
	public String getAuthor() {
		return "Murf";
	}

	private Point getChoicePoint(final int index) {
		final int col = index % 2;
		final int row = index / 2;
		final int x = 42 + 15 + col * 164 + random(0, 149 - 30);
		final int y = 76 + 15 + row * 78 + random(0, 63 - 30);
		return new Point(x, y);
	}

	@Override
	public String getName() {
		return "Improved Rewards Box";
	}

	@Override
	public double getVersion() {
		return 1.0;
	}

	private Point getXPPoint(final int index) {
		if (index >= 21) {
			final int col = (index - 21) % 3;
			final int x = 167 + col * 60 + random(0, 54);
			final int y = 250 + random(0, 53);
			return new Point(x, y);
		}
		final int col = index % 7;
		final int row = index / 7;
		final int x = 51 + col * 60 + random(0, 54);
		final int y = 84 + row * 55 + random(0, 53);
		return new Point(x, y);
	}

	@Override
	public int loop() {
		if (wantedSkill != -1
				&& (inventoryContains(lampID) || inventoryContains(bookOfKnowledgeID))) {
			if (getInterface(XPIF).isValid()) {
				final RSInterfaceChild confFace = getChildInterface(XPIF,
						XPConfirmIF);
				moveMouse(getXPPoint(wantedSkill));
				wait(random(600, 800));
				clickMouse(true);
				moveMouse(confFace.getAbsoluteX()
						+ random(0, confFace.getWidth()), confFace
						.getAbsoluteY()
						+ random(0, confFace.getHeight()));
				wait(random(600, 800));
				clickMouse(true);
				return random(800, 1000);
			} else {
				atInventoryItem(inventoryContains(lampID) ? lampID
						: bookOfKnowledgeID, inventoryContains(lampID) ? "Rub"
						: "Read");
				return random(1000, 1500);
			}
		}
		if (inventoryContains(mysteryBoxID)) {
			atInventoryItem(mysteryBoxID, "Open");
			return random(1500, 2000);
		}
		yourChoices = new ArrayList<String>();
		if (yourChoice == null || yourChoice.equals("")) {
			log.severe("Please set what kind of reward you would like to get.");
			log
					.severe("Add a new account property called \"reward\" and type the ID of the item you want.");
			log
					.severe("You can choose many different types of rewards by separating the IDs with");
			log.severe("dots (.). Here is an example: 1621.447.453");
			log
					.severe("Your first choice is emeralds, if there isn't any, then choose mithril ore,");
			log.severe("then coal ore.");
			log
					.severe("If you want to get a XP Item then just type XP instead of a ID and then");
			log.severe("what kind of skill you want XP in.");
			log.severe("Example: 1621.XP/mining.447.");
			log
					.severe("Again, first choice is emeralds, then XP in mining and then mithril ore.");
			log
					.severe("As you can see, you seperate \"XP\" and what kind of XP you want with a slash(/).");
			log.severe("You can also choose emote, costume or mystery box.");
			log.severe("emote.xp/mining.custume.1621.mystery");
		} else {
			final StringTokenizer tempST = new StringTokenizer(yourChoice, ".");
			while (tempST.hasMoreTokens()) {
				yourChoices.add(tempST.nextToken());
			}
		}
		if (getInterface(boxIF).isValid()) {
			rewards = new ArrayList<String>();
			wanted = -1;
			wantedSkill = -1;
			final RSInterfaceComponent[] tempChoices = getChildInterface(boxIF,
					boxChoicesIF).getComponents();
			for (final RSInterfaceComponent tempChoice : tempChoices) {
				if (tempChoice.getComponentID() != -1) {
					rewards.add(tempChoice.getComponentID() + "");
				} else if (tempChoice.getComponentName().toLowerCase()
						.contains("emote")) {
					rewards.add("emote");
				} else if (tempChoice.getComponentName().toLowerCase()
						.contains("costume")) {
					rewards.add("costume");
				}
			}
			for (int i = 0; i < yourChoices.size(); i++) {
				if (yourChoices.get(i).toString().substring(0, 3).toLowerCase()
						.equals("xp/")) {
					for (int a = 0; a < skills.length; a++) {
						if (yourChoices.get(i).toString().substring(3)
								.toLowerCase().equals(skills[a].toLowerCase())) {
							for (int x = 0; x < rewards.size(); x++) {
								if (rewards.get(x).equals(
										bookOfKnowledgeID + "")
										|| rewards.get(x).equals(lampID + "")) {
									wantedSkill = a;
									wanted = x;
									break;
								}
							}
						}
						if (wanted != -1) {
							break;
						}
					}
				} else if (yourChoices.get(i).toString().toLowerCase().equals(
						"mystery")) {
					for (int a = 0; a < rewards.size(); a++) {
						if (rewards.get(a).equals(mysteryBoxID + "")) {
							wanted = a;
							break;
						}
					}
				} else {
					for (int a = 0; a < rewards.size(); a++) {
						if (yourChoices.get(i).equals(
								rewards.get(a).toString().toLowerCase())) {
							wanted = a;
							break;
						}
					}
				}
				if (wanted != -1) {
					break;
				}
			}
			final RSInterfaceChild confFace = getChildInterface(boxIF,
					boxConfirmIF);
			if (wanted != -1) {
				if (wanted <= 5) {
					moveMouse(getChoicePoint(wanted));
					wait(random(600, 800));
					clickMouse(true);
				} else if (rewards.size() <= 12) {
					final RSInterfaceChild tempFace = getChildInterface(boxIF,
							boxScrollIF);
					moveMouse(tempFace.getAbsoluteX()
							+ random(0, tempFace.getWidth()), tempFace
							.getAbsoluteY()
							+ tempFace.getHeight() - random(0, 10));
					wait(random(400, 500));
					for (int i = 0; i < 25; i++) {
						clickMouse(true);
						wait(50);
					}
					moveMouse(getChoicePoint(wanted - (rewards.size() - 6)
							- rewards.size() % 2));
					wait(random(600, 800));
					clickMouse(true);
				} else {
					log
							.severe("There are more than 12 options, check it out plzz");
					stopAllScripts();
				}
			} else {
				log("Couldn't find anything you would like to have, choosing coins.");
				moveMouse(getChoicePoint(0));
				wait(random(600, 800));
				clickMouse(true);
			}
			moveMouse(confFace.getAbsoluteX() + random(0, confFace.getWidth()),
					confFace.getAbsoluteY() + random(0, confFace.getHeight()));
			wait(random(600, 800));
			clickMouse(true);
			return random(3000, 3500);
		}
		if (activateCondition()) {
			atInventoryItem(boxID, "Open");
			return random(1500, 2000);
		}
		return -1;
	}

}
